import 'dart:async';

import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class PullToRefreshTestPage extends StatefulWidget {
  @override
  _PullToRefreshTestPageState createState() => new _PullToRefreshTestPageState();
}

class _PullToRefreshTestPageState extends State<PullToRefreshTestPage> {
//  RefreshMode  refreshing = RefreshMode.idle;
//  LoadMode loading = LoadMode.idle;
  RefreshController _refreshController;
  List<Widget> data = [];
  void _getDatas() {
    for (int i = 0; i < 14; i++) {
      data.add(new Card(
        margin:
        new EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
        child: new Center(
          child: new Text('Data $i'),
        ),
      ));
    }
  }

  void enterRefresh() {
    _refreshController.requestRefresh(true);
  }

  void _onOffsetCallback(bool isUp, double offset) {
    // if you want change some widgets state ,you should rewrite the callback
  }

  @override
  void initState() {
    // TODO: implement initState
    _getDatas();
    _refreshController = new RefreshController();
    super.initState();
  }

  Widget _headerCreate(BuildContext context, int mode) {
    return new ClassicIndicator(
      mode: mode,
      refreshingText: "",
      idleIcon: new Container(),
      idleText: "Load more...",
    );
  }

  Widget _footerCreate(BuildContext context,int mode){
    print('mode: $mode');
    return new ClassicIndicator(mode: mode);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("下拉刷新，上拉加载"),),
      body: new Container(
          child: new SmartRefresher(
              enablePullDown: true,
              enablePullUp: true,
              controller: _refreshController,
              footerBuilder: _footerCreate,
              onRefresh: (up) {
                if (up) {
                  print("下拉刷新 $up");
                  new Future.delayed(const Duration(milliseconds: 2009))
                      .then((val) {
                    data.add(new Card(
                      margin: new EdgeInsets.only(
                          left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
                      child: new Center(
                        child: new Text('Data ${DateTime.now().toLocal().toString()}'),
                      ),
                    ));

                    _refreshController.scrollTo(
                        _refreshController.scrollController.offset + 50.0);
                    _refreshController.sendBack(true, RefreshStatus.idle);
                    setState(() {});
//                refresher.sendStatus(RefreshStatus.completed);
                  });
                }
                else {
                print("加载更多 $up");
                new Future.delayed(const Duration(milliseconds: 2009))
                      .then((val) {
                    data.add(new Card(
                      margin: new EdgeInsets.only(
                          left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
                      child: new Center(
                        child: new Text('Data ${DateTime.now().toLocal().toString()}'),
                      ),
                    ));
                    setState(() {});
                    _refreshController.sendBack(false, RefreshStatus.idle);
                  });
                }
              },
              onOffsetChange: _onOffsetCallback,
              child: new ListView.builder(
                reverse: true,
                itemExtent: 100.0,
                itemCount: data.length,
                itemBuilder: (context, index) => new Item(),
              ))),
    ) ;
  }
}

class Item extends StatefulWidget {
  @override
  _ItemState createState() => new _ItemState();
}

class _ItemState extends State<Item> {
  @override
  Widget build(BuildContext context) {
    return new Card(
      margin:
      new EdgeInsets.only(left: 10.0, right: 10.0, top: 5.0, bottom: 5.0),
      child: new Center(
        child: new Text('Data ${DateTime.now().toLocal().toString()}'),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}